<@layout.skeleton>

<@app.jsLink 'index.js'/>

<#assign failedTasks = []/> <#-- Stores info about failed tasks to build stacktrace modals at the end of this page -->

<table class="table table-bordered table-hover">
    <thead>
        <tr>
            <th nowrap>Revision</th>
            <th nowrap>Date</th>
            <th nowrap>Author</th>
            <th style="width: 100%">Comment</th>
        </tr>
    </thead>

    <#if currentProject??>
        <#assign projectList = [currentProject]/>
    <#else>
        <#assign projectList = projects/>
    </#if>

    <#global hasEntries = false/>
    <tbody>
        <#list projectList as project>
            <#if (authorsFilter?size == 0) || isInProject(project, authorsFilter)>
                <#assign currentTaskClass = ''/>
                <#if currentTask??>
                    <#assign currentTaskClass = layout.defineTaskClass(currentTask)/>
                </#if>
                <#if !currentTaskClass?has_content>
                    <#assign currentTaskClass = layout.defineProjectClass(project)/>
                </#if>
                <#if (mergedFilter?number > 0) || currentTaskClass != 'merged'>
                    <#global hasEntries = true/>
                    <@renderProject project currentTaskClass/>
                </#if>
            </#if>
        </#list>
        <#if !hasEntries>
            <tr>
                <td colspan="4">
                    <div class="text-center">No entries were found</div>
                </td>
            </tr>
        </#if>
    </tbody>
</table>

<div class="legend">
    <div>
        <div class="project empty">&nbsp;</div> - empty
    </div>
    <div>
        <div class="project merged">&nbsp;</div> - merged
    </div>
    <div>
        <div class="project unmerged">&nbsp;</div> - unmerged
    </div>
    <div>
        <div class="project failed">&nbsp;</div> - failed
    </div>
</div>

<#-- Stacktrace modals -->
<#list failedTasks as failed>
    <div id="${failed.task.id}-stacktrace-modal" class="modal hide stacktrace-modal">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h3><a href="<@spring.url '/${failed.project.shortName}/${failed.task.shortName}/edit'/>">${failed.task.name}</a></h3>
        </div>
        <div class="modal-body">
        ${failed.task.stackTrace?replace('\r\n\t', '<br/>')}
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal">Close</button>
        </div>
    </div>
</#list>

</@layout.skeleton>


<#-- macro definitions ------------------------------------------------------------------------------------------------>

<#macro renderProject project class>
    <tr name="${project.shortName}" class="project ${class}">
        <td colspan="4">
            <div class="pull-right">
                <div>${tasksInProject(project, authorsFilter)} tasks</div>
                <div>${revisionsInProject(project, authorsFilter)} revisions</div>
            </div>
            <div class="text-center"><a href="<@spring.url '/${project.shortName}'/>">${project.name}</a></div>
        </td>
    </tr>
    <#if currentTask??>
        <#assign taskList = [currentTask]/>
    <#else>
        <#assign taskList = project.tasks/>
    </#if>
    <#list taskList as task>
        <#if (authorsFilter?size == 0) || isInTask(task, authorsFilter)>
            <#assign taskClass = layout.defineTaskClass(task)/>
            <#if (mergedFilter?number > 1) || taskClass != 'merged'>
                <@renderTask project task taskClass/>
            </#if>
        </#if>
    </#list>
</#macro>

<#macro renderTask project task class>
    <tr class="task ${class}">
        <td colspan="4">
            <div class="pull-left"><a class="scroller" href="#${project.shortName}">${project.name}</a></div>
            <div class="pull-right">
                <#if task.executed??>
                    <#assign executed = task.executed?datetime>
                <#else>
                    <#assign executed = 'never'>
                </#if>
                <div>
                    updated: ${executed}
                </div>
                <div>
                    <#if task.stackTrace??>
                        <a id-stacktrace-modal="${task.id}-stacktrace-modal" href="javascript:">[ERROR]</a>
                        <#assign failedTasks = failedTasks + [{"project": project, "task": task}]/>
                    <#else>
                        ${revisionsInTask(task, authorsFilter)} revisions
                    </#if>
                </div>
            </div>
            <div class="text-center"><a href="<@spring.url '/${project.shortName}/${task.shortName}'/>">${task.name}</a></div>
        </td>
    </tr>
    <#list task.unmergedRevisions as revision>
        <#if (authorsFilter?size == 0) || containsCaseInsensitive(authorsFilter, revision.author)>
            <@renderRevision revision/>
        </#if>
    </#list>
</#macro>

<#macro renderRevision revision>
    <tr>
        <td nowrap>
            <#if revisionViewer?has_content>
                <a href="${revisionViewer?replace('{0}', revision.number)}" target="_blank">
                    ${revision.number}
                </a>
            <#else>
                ${revision.number}
            </#if>
        </td>
        <td nowrap>${revision.timestamp?datetime}</td>
        <td nowrap><a href="<@buildUrl revision.author/>"$>${revision.author}</a></td>
        <td>
            <#assign comment = revision.comment?replace('\n', '<br/>')/>
            <#if issuePattern?has_content && issueViewer?has_content>
                <#assign issueIds = revision.comment?matches(issuePattern)/>
                <#if (issueIds?size > 0)>
                    <#assign uniqueIssueIds = removeDuplicates(issueIds)/>
                    <#list uniqueIssueIds as issueId>
                        <#assign comment = comment?replace(issueId, '<a target="_blank" href="' + issueViewer?replace('{0}', issueId) + '">' + issueId + '</a>')/>
                    </#list>
                    ${comment}
                <#else>
                    ${comment}
                </#if>
            <#else>
                ${comment}
            </#if>
        </td>
    </tr>
</#macro>

<#macro buildUrl author=''>
    <#compress>
        <#assign path = '/'/>
        <#if currentProject??>
            <#assign path = path + currentProject.shortName/>
        </#if>
        <#if currentTask??>
            <#assign path = path + '/' + currentTask.shortName/>
        </#if>

        <#if author?has_content>
            <@spring.url path + '?author=' + author?url/>
        <#else>
            <@spring.url path/>
        </#if>
    </#compress>
</#macro>


<#-- functions --------------------------------------------------------------------------------------------------------->

<#function isInProject project authors>
    <#list project.tasks as task>
        <#if isInTask(task, authors)>
            <#return true/>
        </#if>
    </#list>
    <#return false/>
</#function>

<#function isInTask task authors>
    <#list task.unmergedRevisions as revision>
        <#list authors as author>
            <#if revision.author?lower_case == author?lower_case>
                <#return true/>
            </#if>
        </#list>
    </#list>
    <#return false/>
</#function>

<#function tasksInProject project authors>
    <#if authors?size == 0>
        <#return project.tasks?size/>
    </#if>
    <#assign count = 0/>
    <#list project.tasks as task>
        <#if isInTask(task, authors)>
            <#assign count = count + 1/>
        </#if>
    </#list>
    <#return count/>
</#function>

<#function revisionsInTask task authos>
    <#if (authors?size == 0)>
        <#return task.unmergedRevisions?size/>
    </#if>
    <#assign count = 0/>
    <#list task.unmergedRevisions as revision>
        <#if authors?seq_contains(revision.author)>
            <#assign count = count + 1/>
        </#if>
    </#list>
    <#return count/>
</#function>

<#function revisionsInProject project authors>
    <#assign count = 0/>
    <#list project.tasks as task>
        <#assign count = count + revisionsInTask(task, authors)/>
    </#list>
    <#return count/>
</#function>

<#function removeDuplicates array>
    <#assign map = {}/>
    <#list array as element>
        <#assign map = map + {element: element}/>
    </#list>
    <#return map?keys/>
</#function>

<#function containsCaseInsensitive seq value>
    <#assign valueLowerCase = value?lower_case/>
    <#list seq as element>
        <#if element?lower_case == valueLowerCase>
            <#return true>
        </#if>
    </#list>
    <#return false/>
</#function>